package com.huadao.cart_ccs.data.source.http;

import android.accounts.NetworkErrorException;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;
import com.huadao.cart_ccs.common.ExperimentStageManager;
import com.huadao.cart_ccs.common.ProjectManager;
import com.huadao.cart_ccs.common.RockToast;
import com.huadao.cart_ccs.exception.ExceptionCode;
import com.huadao.cart_ccs.exception.ExceptionType;
import com.huadao.cart_ccs.greendao.GreenDaoManager;
import com.huadao.cart_ccs.greendao.entity.ExceptionInfo;

import org.greenrobot.greendao.async.AsyncSession;
import org.json.JSONException;

import java.io.InterruptedIOException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.util.Date;
import java.util.concurrent.TimeoutException;

public abstract class RockBaseObserver<T> extends BaseObserver<T> {

    private static final String TAG = "RockBaseObserver";

    private boolean isNeedProgress;
    private String loadingMsg;

    //    private ExceptionInfoDao exceptionInfoDao;
    private AsyncSession asyncSession;

    public RockBaseObserver() {
        this(null, null);
    }

    public RockBaseObserver(BaseImpl mBaseImpl) {
        this(mBaseImpl, null);
    }

    public RockBaseObserver(BaseImpl mBaseImpl, String loadingMsg) {
        super(mBaseImpl);
        this.loadingMsg = loadingMsg;
        if (TextUtils.isEmpty(loadingMsg)) {
            this.isNeedProgress = false;
        } else {
            this.isNeedProgress = true;
        }
        asyncSession = GreenDaoManager.getInstance().getAsyncNoCacheSession();
    }

    @Override
    protected void onBaseError(Throwable t) {
        byte exceptionCode;
        StringBuilder sb = new StringBuilder();
//        sb.append("请求失败：");
        if (t instanceof NetworkErrorException || t instanceof UnknownHostException || t instanceof ConnectException) {
            sb.append("网络异常");
            exceptionCode = ExceptionCode.NETWORK_CONNECT;
        } else if (t instanceof SocketTimeoutException || t instanceof InterruptedIOException || t instanceof TimeoutException) {
            sb.append("请求超时");
            exceptionCode = ExceptionCode.NETWORK_TIMEOUT;
        }
//        else if(t instanceof JsonSyntaxException){
//            sb.append("请求不合法");
//            exceptionCode=ExceptionCode.NETWORK_JSON;
//        }
        else if (t instanceof JsonSyntaxException || t instanceof JsonParseException || t instanceof JSONException || t instanceof ParseException) {
            sb.append("解析错误");
            exceptionCode = ExceptionCode.NETWORK_JSON;
        } else if (t instanceof ApiException) {
            sb.append(t.getLocalizedMessage());
            exceptionCode = ExceptionCode.NETWORK_API;
        } else {
            sb.append(t.getLocalizedMessage());
            exceptionCode = ExceptionCode.NETWORK_OTHER;
        }
        if (t.getLocalizedMessage().equals("The mapper function returned a null value.")) {
            //服务器返回data为null
            onResponseNullData();
        } else {
            RockToast.showToastSafe(sb.toString());
            Log.e(TAG, "onBaseError: " + sb.toString());

            //保存错误到数据库
            long ProjectId = ProjectManager.getInstance().getProjectId();
            long stageId = ExperimentStageManager.getInstance().getStageId();
            asyncSession.insert(new ExceptionInfo(null, new Date(), ProjectId, stageId, exceptionCode, sb.toString(), ExceptionType.ERROR_NETWORK));
        }
    }

    protected void onResponseNullData() {
        Log.e(TAG, "onResponseNullData");
    }

    @Override
    protected boolean isNeedProgressDialog() {
        return isNeedProgress;
    }

    @Override
    protected String getLoadingMsg() {
        return loadingMsg;
    }
}
